{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Based on this:\n",
    "\n",
    "https://towardsdatascience.com/serverless-bert-with-huggingface-and-aws-lambda-625193c6cc04"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "\n",
    "def upload_model(model_path='', s3_bucket='', key_prefix='', aws_profile='default'):\n",
    "    s3 = boto3.session.Session(profile_name=aws_profile)\n",
    "    client = s3.client('s3')\n",
    "    client.upload_file(model_path, s3_bucket, key_prefix)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note:  `pytorch_model.bin` from https://towardsdatascience.com/serverless-bert-with-huggingface-and-aws-lambda-625193c6cc04 must be tar gz'd into `pytorch_model.tar.gz`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "upload_model(model_path='./pytorch_model.tar.gz',\n",
    "             s3_bucket='sagemaker-us-east-1-835319576252',\n",
    "             key_prefix='serverless-bert/pytorch_model.tar.gz')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Install `Serverless` CLI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "!curl -o- -L https://slss.io/install | bash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    "export PATH=\"$HOME/.serverless/bin:$PATH\"\n",
    "\n",
    "cd serverless-bert-with-huggingface-aws-lambda && sls plugin install -n serverless-python-requirements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!pygmentize serverless-bert-with-huggingface-aws-lambda/serverless.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pygmentize serverless-bert-with-huggingface-aws-lambda/handler.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Deploy Lambda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "\n",
    "export PATH=\"$HOME/.serverless/bin:$PATH\"\n",
    "\n",
    "cd serverless-bert-with-huggingface-aws-lambda && serverless deploy "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
